/**
 * 属性管理相关接口
 */
import api from '@/utils/request';

// ==================== 属性基础数据 ====================

/**
 * 获取属性分类（用于级联下拉）
 */
export const getPropertyCategories = (params, showLoading = true) =>
  api.get('/item-center/property/option', params, showLoading);

/**
 * 获取属性层级树
 */
export const getPropertyHierarchy = (params, showLoading = true) =>
  api.get('/item-center/property/hierarchy', params, showLoading);

/**
 * 获取属性详情
 */
export const getPropertyDetail = (propertyCode, showLoading = true) =>
  api.get(`/item-center/property/${propertyCode}`, {}, showLoading);

/**
 * 获取属性列表
 */
export const getPropertyList = (params, showLoading = true) =>
  api.get('/item-center/property/list', params, showLoading);

/**
 * 获取SKU属性类型
 */
export const getSKUPropertyTypes = (showLoading = true) =>
  api.get('/erp/systemConfig/item/SKU_Property_TYPE', {}, showLoading);

/**
 * 获取属性类别（SPECIFICATION_TYPE 枚举）
 */
export const getSpecificationTypeOptions = (params, showLoading = true) =>
  api.get('/erp/systemConfig/item/SPECIFICATION_TYPE', params, showLoading);

// ==================== 属性业务操作 ====================

/**
 * 启用/停用属性
 */
export const updatePropertyStatus = (data, showLoading = true) =>
  api.put('/item-center/property/disable', data, showLoading);

/**
 * 导出属性数据
 */
export const exportPropertyList = (params = {}) => {
  const searchParams = new URLSearchParams();
  Object.keys(params).forEach((key) => {
    const value = params[key];
    if (value !== undefined && value !== null) {
      searchParams.append(key, value);
    }
  });
  const query = searchParams.toString();
  const url = `${process.env.VUE_APP_MOCK}/item-center/property/export${
    query ? `?${query}` : ''
  }`;
  return api.get(url, {}, false, 'blob');
};

/**
 * 创建属性
 */
export const createProperty = (data, showLoading = true) =>
  api.post('/item-center/property/create', data, showLoading);

/**
 * 更新属性
 */
export const updateProperty = (data, showLoading = true) =>
  api.put('/item-center/property/update', data, showLoading);

/**
 * 更新属性值状态
 */
export const updatePropertyValueStatus = (data, showLoading = true) =>
  api.put('/item-center/property/item/disable', data, showLoading);

/**
 * 删除属性值
 */
export const deletePropertyValue = (valueCode, showLoading = true) =>
  api.delete(`/item-center/property/${valueCode}`, {}, showLoading);

/**
 * attributeManagement 模块导出
 */
export const attributeAPI = {
  getPropertyCategories,
  getPropertyHierarchy,
  getPropertyDetail,
  getPropertyList,
  getSKUPropertyTypes,
  getSpecificationTypeOptions,
  updatePropertyStatus,
  exportPropertyList,
  createProperty,
  updateProperty,
  updatePropertyValueStatus,
  deletePropertyValue,
};

export default attributeAPI;
